#[cfg(unix)]
use tokio_serial::SerialStream;

#[cfg(unix)]
use crate::error::AppErr;

#[cfg(unix)]
pub fn fd_lock(stream: &SerialStream) -> Result<(), AppErr> {
    use libc::{flock, LOCK_EX, LOCK_NB};
    use std::os::fd::AsRawFd;

    use crate::error::new_err;

    let fd = stream.as_raw_fd();

    unsafe {
        let ret = flock(fd, LOCK_EX | LOCK_NB);
        if ret < 0 {
            return Err(new_err("串口已经被打开"));
        }
    }
    Ok(())
}
